//! @Author: DengLibin
//! @Date: Create in 2023-11-06 15:05:39
//! @Description:

use config::AppConfig;
use global::{check_result, GlobalResult};
use log::info;
use rust_common::log_util;

use std::net::SocketAddr;

mod config;
mod controller;
mod dao;
mod entity;
mod global;
mod midware;
mod router;
mod service;

/// @Author: DengLibin
/// @Date: Create in 2023-11-06 15:10:01
/// @Description: 日志初始化
pub fn init_log() -> GlobalResult<()> {
    let r = log_util::init_log(r"log4rs.yaml");
    global::check_result(r)
}

/// @Author: DengLibin
/// @Date: Create in 2023-11-06 15:10:09
/// @Description: 初始化
pub fn init() -> GlobalResult<AppConfig> {
    init_log()?;
    config::read_yaml_config("application.yml")
}

#[tokio::main]
async fn main() -> GlobalResult<()> {
    //初始化
    let config = init()?;
    info!("初始化日志完成");

    // build our application with a route
    let (config, router) = router::new(config)?;
    // run it
    let addr = SocketAddr::from(([0, 0, 0, 0], config.port as u16));
    info!("====================================>监听端口:{}", addr);

    let listener = check_result(tokio::net::TcpListener::bind("0.0.0.0:3000").await)?;
    check_result(axum::serve(listener, router).await)?;

    Ok(())
}
